-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Quicksort #144
base: main
Are you sure you want to change the base?
Quicksort #144
Conversation
51f26e4
to
538e552
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good; thanks a lot. I played around with it manually and read the code. I couldn't find a bug.
Two questions:
- Did you test it in Rust with some more inputs?
- Can you add it to the std lib if @bobbinth agrees?
examples/quicksort.masm
Outdated
#! Input stack: [ n ] | ||
#! Input advice: [ element_1, element_2, ..., element_n ] | ||
#! | ||
#! Output stack: The 16 highest elements in descending order |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I think it also outputs more elements if there are any, right? At least in the playground, I get more results and the same should be the case if I use the API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
I tested it on the command line with various combinations of input. I haven't done any performance testing on it, though - I don't think I went above 16 elements.
Sure, though we should change it to be more flexible if it is to be added to the standard library. For one thing, it should be able to sort an array (i.e., a sequential block of memory) starting from any address, not just from address 0. I'm also not sure we can add randomization when choosing the pivot element, so the algorithm runs in O(n^2) time. This means that we could just as well just use bubblesort, which is a simpler algorithm. |
Kudos, SonarCloud Quality Gate passed! |
A non-randomized quicksort algorithm.
The algorithm could probably be made more robust. Currently, the assumption is that the
n
input elements are located in the firstn
memory addresses, which won't be true in general if called from another program.I have not looked into how to pick the pivot element at random.